package com.yuncheng.spcyApi.controller;

import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.yuncheng.spcyApi.mapper.SpcySjxdJbxxMapper;
import io.swagger.annotations.*;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.SpcySqsxBaiscMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.base.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.*;

/**
 * @Description: 审评查验-文件模板
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "审评查验-文件模板库接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyRsWjmb")
public class SpcyRsWjmbController extends BaseController<SpcyRsWjmb, ISpcyRsWjmbService> {

    private static final Logger log = LoggerFactory.getLogger(SpcyRsWjmbController.class);

    @Resource
    private ISpcyRsWjmbService spcyRsWjmbService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    @Lazy
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    private HnCaUtils hnCaUtils;

    @Resource
    private ISpcySqsxWjjlService spcySqsxWjjlService;

    @Resource
    private BusService busService;

    @Resource
    private SpcySqsxBaiscMapper spcySqsxBaiscMapper;

    @Resource
    private SpcySjxdJbxxMapper spcySjxdJbxxMapper;

    @Log(title = "文件模板-分页列表查询")
    @ApiOperation(value = "文件模板-分页列表查询")
    @GetMapping(value = "/queryPageList")
    public HttpResult<?> queryPageList(SpcyRsWjmb query, PageVo pageVo) {
        Page<SpcyRsWjmb> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<SpcyRsWjmb> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isBlank(query.getFywflcode())){
            queryWrapper.isNull(SpcyRsWjmb::getFsqsxid);
        }else {
            queryWrapper.eq(SpcyRsWjmb::getFywflcode, query.getFywflcode());
        }

        if (StringUtils.isNotBlank(query.getFmbbt())){
            queryWrapper.and(
                    q->q.like(SpcyRsWjmb::getFmbbt, query.getFmbbt())
                    .or()
                    .like(SpcyRsWjmb::getFmblx1, query.getFmbbt())
            );
        }

        /*queryWrapper.like(StringUtils.isNotEmpty(spcyRsWjmb.getFmbbt()),SpcyRsWjmb::getFmbbt,query.getFmbbt());
        queryWrapper.like(StringUtils.isNotEmpty(spcyRsWjmb.getFmblx1()),SpcyRsWjmb::getFmblx1,query.getFmblx1());*/

        queryWrapper.like(StringUtils.isNotEmpty(query.getFmbzt()),SpcyRsWjmb::getFmbzt,query.getFmbzt());

        queryWrapper.orderByDesc(SpcyRsWjmb::getUpdateTime);
        queryWrapper.orderByDesc(SpcyRsWjmb::getCreateTime);
        IPage<SpcyRsWjmb> pageList = spcyRsWjmbService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "审评查验-文件模板-批量删除")
    @ApiOperation(value = "审评查验-文件模板-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam(value = "ids",defaultValue = "") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("参数为空");
        }
        List<String> idsList = StringPatternUtils.getListByIds(ids);
        List<SpcyRsWjmb> spcyQyJbxxList = spcyRsWjmbService.listByIds(idsList);
        if (CollectionUtils.isEmpty(spcyQyJbxxList)){
            return HttpResult.error("没有想匹配的模板数据！");
        }
        String operContent = "";
        for (SpcyRsWjmb wjmb : spcyQyJbxxList){
            String id = wjmb.getId();
            spcyRsWjmbService.removeById(id);

            localFileUploadUtils.deleteLocalWjmbFileUrl(wjmb.getFcflj()); // 删除文件

            operContent = "删除文件模板信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(wjmb);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_RS_WJMB,operContent,id,"批量删除文件模板", SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "审评查验-文件模板-通过id查询")
    @ApiOperation(value = "审评查验-文件模板-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        SpcyRsWjmb spcyRsWjmb = spcyRsWjmbService.getById(id);
        return HttpResult.ok(spcyRsWjmb);
    }

    @Log(title = "下载模板文件-通过模板文件id")
    @ApiOperation(value = "下载模板文件-通过模板文件id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "文件模板id",required = true)
    })
    @GetMapping(value = "/downWjmlById")
    public void downWjmlById(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response) {
        if (StringUtils.isEmpty(id)){
            log.error("模板文件id不能为空");
            return;
        }
        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(id);
        if (wjmb == null){
            log.error("文件模板id为-{}的文件记录不存在,请重试",id);
            return;
        }
        localFileUploadUtils.getLocalWjmbFileURL(wjmb.getFcflj(),wjmb.getFwjccmc(),response);
    }


    @Log(title = "新增模板文件")
    @ApiOperation(value = "新增模板文件")
    @PostMapping(value = "addWjmb")
    public HttpResult<?> addWjmb(@RequestBody SpcyRsWjmb spcyRsWjmb){
        if (StringUtils.isBlank(spcyRsWjmb.getId())) {
            if (StringUtils.isBlank(spcyRsWjmb.getFywflcode())) {
                return HttpResult.error("业务分类编码不能为空");
            }
            spcyRsWjmb.setId(GetUuIdUtils.ReplaceUuId());
            if (StringUtils.isBlank(spcyRsWjmb.getFmblx2())) {
                spcyRsWjmb.setFmblx2("默认");
            }
            spcyRsWjmb.setCreateTime(new Date());
            spcyRsWjmb.setUpdateTime(new Date());
            if (StringUtils.isNotBlank(spcyRsWjmb.getFmblx1()) && spcyRsWjmb.getFmblx1().equals(WjmlConstant.xcjcbg)){
                if (StringUtils.isBlank(spcyRsWjmb.getFmsmbbt())) {
                    spcyRsWjmb.setFmsmbbt(SpcyCommonConstant.WJMB_MSMBNR_TITLE);
                }
                if (StringUtils.isBlank(spcyRsWjmb.getFjcxmbt())){
                    spcyRsWjmb.setFjcxmbt(SpcyCommonConstant.WJMB_JCXM_TITLE);
                }
            }
            spcyRsWjmbService.save(spcyRsWjmb);

            return HttpResult.ok(spcyRsWjmb);
        }else {
            SpcyRsWjmb wjmb = spcyRsWjmbService.getById(spcyRsWjmb.getId());
            if (wjmb != null) {
                wjmb.setFmbbt(spcyRsWjmb.getFmbbt());
                wjmb.setFmblx1(spcyRsWjmb.getFmblx1());

                if (StringUtils.isBlank(spcyRsWjmb.getFmblx2())){
                    wjmb.setFmblx2("默认");
                }else {
                    wjmb.setFmblx2(spcyRsWjmb.getFmblx2());
                }

                wjmb.setFmblx3(spcyRsWjmb.getFmblx3());
                wjmb.setFmblx4(spcyRsWjmb.getFmblx4());
                wjmb.setFmbzt(spcyRsWjmb.getFmbzt());
                wjmb.setFmsmbnr(spcyRsWjmb.getFmsmbnr());
                wjmb.setFmsmbbt(spcyRsWjmb.getFmsmbbt());
                wjmb.setFjcxmbt(spcyRsWjmb.getFjcxmbt());
                wjmb.setUpdateTime(new Date());

                if (StringUtils.isNotBlank(wjmb.getFmblx1()) && wjmb.getFmblx1().equals(WjmlConstant.xcjcbg)){
                    if (StringUtils.isBlank(wjmb.getFmsmbbt())) {
                        wjmb.setFmsmbbt(SpcyCommonConstant.WJMB_MSMBNR_TITLE);
                    }
                    if (StringUtils.isBlank(wjmb.getFjcxmbt())){
                        wjmb.setFjcxmbt(SpcyCommonConstant.WJMB_JCXM_TITLE);
                    }
                }

                spcyRsWjmbService.updateById(wjmb);

                return HttpResult.ok(wjmb);
            }else {
                return HttpResult.error("该文件模板不存在，请重试");
            }
        }
    }

    @Log(title = "上传模板文件")
    @ApiOperation(value = "上传模板文件")
    @PostMapping(value = "addRsWjmb")
    public HttpResult<?> addRsWjmb(@RequestPart("file") MultipartFile file, SpcyRsWjmb spcyRsWjmb){
        if (StringUtils.isBlank(spcyRsWjmb.getId())){
            return HttpResult.error("文件模板id不能为空");
        }
        if (file.isEmpty()){
            return HttpResult.error("文件不能为空，请上传文件");
        }

        String originalFilename = file.getOriginalFilename();
        // 文件类型
        String wjlx = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 必须上传 .docx 文件
        if (!wjlx.equals(SpcyConstant.PRINT_FILE_TYPE_DOCX)
                && !wjlx.equals(SpcyConstant.PRINT_FILE_TYPE_DOC)
                && !wjlx.equals(".xls") && !wjlx.equals(".xlsx")){
            return HttpResult.error("请重新上传文件，文件类型为【.docx】【.doc】【.xls】【.xlsx】");
        }

        // 文件名
        String fileName = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));

        String sjmc = fileName + GetUuIdUtils.ReplaceUuId();
        String wjccmc = sjmc + wjlx;
        String nowDay = DateConversionUtils.DateToMonthTwo(new Date()); // 获取当前日期 yyyy-MM-dd
        String newDir = "";
        if (StringUtils.isNotBlank(spcyRsWjmb.getFywflcode())){
            newDir = "/"+spcyRsWjmb.getFywflcode();
        }else {
            newDir = "/"+nowDay;
        }

        String cflj = newDir + "/" + wjccmc;

        localFileUploadUtils.uploadLocalWjmbFile(file, sjmc, spcyRsWjmb.getFywflcode()); // 上传文件

        spcyRsWjmb.setFcflj(cflj);
        spcyRsWjmb.setFwjccmc(wjccmc);
        spcyRsWjmb.setFwjlx(wjlx);

        // 旧的记录
        SpcyRsWjmb oldWjmb = spcyRsWjmbService.getById(spcyRsWjmb.getId());
        if (oldWjmb != null && StringUtils.isNotBlank(oldWjmb.getFcflj())){
            localFileUploadUtils.deleteLocalWjmbFileUrl(oldWjmb.getFcflj());
        }
        if (StringUtils.isEmpty(oldWjmb.getFmbbt())){
            spcyRsWjmb.setFmbbt(fileName);
        }
        spcyRsWjmb.setUpdateTime(new Date());
        spcyRsWjmbService.updateById(spcyRsWjmb);

        String operContent = "基本信息修改：";

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldWjmb);
        operContent += LogUtils.BeanModifyMsg(spcyRsWjmb,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_RS_WJMB,operContent,spcyRsWjmb.getId(),LogConstant.LOGS_RS_WJMB,SpcyConstant.LOGS_SYS_TYPE_XG);

        HttpResult result = new HttpResult(true);
        result.setMessage("上传文件成功");
        result.setResult(cflj);
        return result;
    }

    @Log(title = "删除模板文件")
    @ApiOperation(value = "删除模板文件")
    @DeleteMapping(value = "deleteWjmb")
    public HttpResult<?> deleteWjmb(@RequestParam(value = "id",defaultValue = "")String id){
        SpcyRsWjmb oldWjmb = spcyRsWjmbService.getById(id);
        if (oldWjmb != null && StringUtils.isNotBlank(oldWjmb.getFcflj())){
            localFileUploadUtils.deleteLocalWjmbFileUrl(oldWjmb.getFcflj());
        }

        return HttpResult.ok("删除成功");
    }


    @Log(title = "模板在线编辑")
    @ApiOperation(value = "模板在线编辑")
    @ApiImplicitParam(name = "id",value = "文件模板id",required = true)
    @GetMapping(value = "openWjbm")
    public void openWjbm(@RequestParam("id")String id,HttpServletResponse response){
        if (StringUtils.isBlank(id)){
            log.error("【模板在线编辑】-文件模板id不能为空");
        }
        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(id);
        if (wjmb == null){
            log.error("【模板在线编辑】-文件模板id:{}的数据不存在",id);
        }
        if (StringUtils.isNotBlank(wjmb.getFcflj())){
            localFileUploadUtils.ejzLocalWjmbFileURl(wjmb.getFcflj(),response);
        }else {
            log.error("【模板在线编辑】- 文件模板的文件不存在");
        }
    }

    @Log(title = "在线编辑-保存模板")
    @ApiOperation(value = "在线编辑-保存模板")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "docfile",value = "保存的文件",required = true),
            @ApiImplicitParam(name = "id",value = "文件模板id",required = true)
    })
    @PostMapping(value = "onLineSaveWjmb")
    public HttpResult<?> onLineSaveWjmb(@RequestParam("docfile") MultipartFile file,String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("文件模板id不能为空");
        }
        SpcyRsWjmb wjmb = spcyRsWjmbService.getById(id);
        if (wjmb == null){
            return HttpResult.error("文件模板数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(wjmb);

        String oldCflj = wjmb.getFcflj(); // 旧文件存放路径

        String originalFilename = file.getOriginalFilename();

        if (originalFilename.lastIndexOf(".") == 0){
            return HttpResult.error("文件不存在这个格式，请联系管理员");
        }

        // 文件类型
//        String wjlx = originalFilename.substring(originalFilename.lastIndexOf("."));
        String wjlx = ".doc";
        // 文件名

        String sjmc = GetUuIdUtils.NotReplaceUuId();
        if (StringUtils.isNotBlank(wjmb.getFmbbt())){
            sjmc = wjmb.getFmbbt() + GetUuIdUtils.NotReplaceUuId();
        }
        String wjccmc = sjmc + wjlx;
        String nowDay = DateConversionUtils.DateToMonthTwo(new Date()); // 获取当前日期 yyyy-MM-dd
        String newDir = "/"+nowDay;
        if (StringUtils.isNotBlank(wjmb.getFywflcode())){
            newDir = "/" + wjmb.getFywflcode();
        }
        String cflj = newDir + "/" + wjccmc;

        localFileUploadUtils.uploadLocalWjmbFile(file, sjmc,wjmb.getFywflcode()); // 上传文件

        wjmb.setFwjlx(wjlx);
        wjmb.setFwjccmc(wjccmc);
        wjmb.setFcflj(cflj);
        wjmb.setUpdateTime(new Date());
        spcyRsWjmbService.updateById(wjmb);

        String log = "模板在线编辑: ";
        log += LogUtils.BeanModifyMsg(wjmb,oldMap);
        spcyLogService.addLogs(LogConstant.LOGS_RS_WJMB,log,id,LogConstant.LOGS_RS_WJMB,SpcyConstant.LOGS_SYS_TYPE_XG);

        localFileUploadUtils.deleteLocalWjmbFileUrl(oldCflj); // 删除旧的文件

        return HttpResult.ok("保存模板成功");
    }

    @Log(title = "查询业务对应文件模板")
    @ApiOperation(value = "查询业务对应文件模板")
    @GetMapping(value = "queryYwflWjmbList")
    public HttpResult<?> queryYwflWjmbList(@RequestParam(value = "fywflcode",defaultValue = "")String fywflcode,
                                           @RequestParam(value = "type",defaultValue = "")String type,
                                           @RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                           @RequestParam(value = "fmbbt",defaultValue = "")String fmbbt){
        String fmblx2 = "";
        if (StringUtils.isNotBlank(sqsxid)) {
            SpcySqsxBasic sqsx = spcySqsxBaiscMapper.selectSqsxById(sqsxid);
            if (sqsx != null){
                fywflcode = Optional.ofNullable(sqsx.getFywflcode()).orElseGet(()->"");
                fmblx2 = Optional.ofNullable(sqsx.getFjclx()).orElseGet(()->"");
            }
        }else {
            if (StringUtils.isBlank(fywflcode)){
                return HttpResult.error("事项参数为空！");
            }
        }
        fywflcode = busService.replaceYwflcode(fywflcode);

        List<SpcyRsWjmb> wjmbList = new ArrayList<>();
        if (StringUtils.isNotBlank(sqsxid) && type.equals(WjmlConstant.xcjcbg)){
            wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                    .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                    .eq(SpcyRsWjmb::getFmblx1, type));

            if (CollectionUtil.isEmpty(wjmbList)){
                wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                        .eq(SpcyRsWjmb::getFywflcode, fywflcode)
                        .eq(SpcyRsWjmb::getFmblx1, type)
                        .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)
                        .isNull(SpcyRsWjmb::getFsqsxid)
                        .isNotNull(SpcyRsWjmb::getFcflj));
            }

        }else {

            wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                    .eq(StringUtils.isNotBlank(fywflcode), SpcyRsWjmb::getFywflcode, fywflcode)
                    .eq(StringUtils.isNotBlank(type), SpcyRsWjmb::getFmblx1, type)
                    .like(StringUtils.isNotBlank(fmblx2), SpcyRsWjmb::getFmblx2, fmblx2)
                    .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)
                    .isNull(SpcyRsWjmb::getFsqsxid)
                    .isNotNull(SpcyRsWjmb::getFcflj));

            if (CollectionUtil.isEmpty(wjmbList)) {
                wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                        .eq(StringUtils.isNotBlank(fywflcode), SpcyRsWjmb::getFywflcode, fywflcode)
                        .eq(StringUtils.isNotBlank(type), SpcyRsWjmb::getFmblx1, type)
                        .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)
                        .isNull(SpcyRsWjmb::getFsqsxid)
                        .isNotNull(SpcyRsWjmb::getFcflj));
            }

        }


        Map map = new HashMap();
        map.put("wjmbList",wjmbList);

        // 是否存在已经选中的模板id
        String xzid = "";

        // 以前选择的模板id
        String oldSelectMbid = busService.getSqsxTypeFile(sqsxid, type);
        if (StringUtils.isNotBlank(oldSelectMbid) && CollectionUtil.isNotEmpty(wjmbList)){
            for (SpcyRsWjmb wjmb : wjmbList){
                if (wjmb.getId().equals(oldSelectMbid)){
                    xzid = wjmb.getId();
                    break;
                }
            }
        }

        map.put("xzid",xzid);

        return HttpResult.ok(map);
    }

    @Log(title = "合并检查-查询业务对应文件模板")
    @ApiOperation(value = "合并检查-查询业务对应文件模板")
    @GetMapping(value = "queryHbjcMbList")
    public HttpResult queryHbjcMbList(@RequestParam(value = "type",defaultValue = "")String type,
                                      @RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                      @RequestParam(value = "fmbbt",defaultValue = "")String fmbbt){

        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }
        if (StringUtils.isBlank(type)){
            return HttpResult.error("模板类型参数为空！");
        }

        String fywflcode = "";
        String fmblx2 = "";
        SpcySqsxBasic sqsx = spcySqsxBaiscMapper.selectSqsxById(sqsxid);
        if (sqsx != null){
            fywflcode = Optional.ofNullable(sqsx.getFywflcode()).orElseGet(()->"");
            fmblx2 = Optional.ofNullable(sqsx.getFjclx()).orElseGet(()->"");
        }

        fywflcode = busService.replaceYwflcode(fywflcode);

        List<SpcyRsWjmb> wjmbList = new ArrayList<>();
        if (type.equals(WjmlConstant.xcjcbg)){
            wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                    .eq(SpcyRsWjmb::getFmblx1, type)
                    .like(SpcyRsWjmb::getFmblx3, sqsxid));

            if (CollectionUtil.isEmpty(wjmbList)){
                wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                        .eq(SpcyRsWjmb::getFywflcode, fywflcode)
                        .eq(SpcyRsWjmb::getFmblx1, type)
                        .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)
                        .isNull(SpcyRsWjmb::getFsqsxid)
                        .isNotNull(SpcyRsWjmb::getFcflj));
            }

        }else {

            wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                    .eq(StringUtils.isNotBlank(fywflcode), SpcyRsWjmb::getFywflcode, fywflcode)
                    .eq(StringUtils.isNotBlank(type), SpcyRsWjmb::getFmblx1, type)
                    .like(StringUtils.isNotBlank(fmblx2), SpcyRsWjmb::getFmblx2, fmblx2)
                    .isNull(SpcyRsWjmb::getFsqsxid)
                    .isNotNull(SpcyRsWjmb::getFcflj));

            if (CollectionUtil.isEmpty(wjmbList)) {
                wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                        .eq(StringUtils.isNotBlank(fywflcode), SpcyRsWjmb::getFywflcode, fywflcode)
                        .eq(StringUtils.isNotBlank(type), SpcyRsWjmb::getFmblx1, type)
                        .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)
                        .isNull(SpcyRsWjmb::getFsqsxid)
                        .isNotNull(SpcyRsWjmb::getFcflj));
            }

        }

        Map map = new HashMap();
        map.put("wjmbList", wjmbList);

        // 是否存在已经选中的模板id
        String xzid = "";

        // 以前选择的模板id
        String oldSelectMbid = busService.getSqsxTypeFile(sqsxid, type);
        if (StringUtils.isNotBlank(oldSelectMbid) && CollectionUtil.isNotEmpty(wjmbList)){
            for (SpcyRsWjmb wjmb : wjmbList){
                if (wjmb.getId().equals(oldSelectMbid)){
                    xzid = wjmb.getId();
                    break;
                }
            }
        }

        map.put("xzid",xzid);

        return HttpResult.ok(map);
    }

    @Log(title = "数据修订-查询业务对应文件模板")
    @ApiOperation(value = "数据修订-查询业务对应文件模板")
    @GetMapping(value = "sjxdFlMbList")
    public HttpResult<?> sjxdFlMbList(@RequestParam(value = "id",defaultValue = "") @ApiParam("数据修订id") String fsjxdid,
                                      @RequestParam(value = "fmbbt",defaultValue = "") @ApiParam("模板标题") String fmbbt){

        if (StringUtils.isBlank(fsjxdid)) {
            return HttpResult.error("修订数据参数为空！");
        }

        String fmblx2 = "";
        String fywflcode = "";
        String type = WjmlConstant.jcfa;

        String sqsxid = spcySjxdJbxxMapper.selectXdSqsxIdById(fsjxdid);
        SpcySqsxBasic sqsx = spcySqsxBaiscMapper.selectSqsxById(sqsxid);
        if (sqsx != null){
            fywflcode = Optional.ofNullable(sqsx.getFywflcode()).orElseGet(()->"");
            fmblx2 = Optional.ofNullable(sqsx.getFjclx()).orElseGet(()->"");
        }

        List<SpcyRsWjmb> wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(StringUtils.isNotBlank(fywflcode), SpcyRsWjmb::getFywflcode, fywflcode)
                .eq(StringUtils.isNotBlank(type), SpcyRsWjmb::getFmblx1, type)
                .like(StringUtils.isNotBlank(fmblx2), SpcyRsWjmb::getFmblx2, fmblx2)
                .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)
                .isNull(SpcyRsWjmb::getFsqsxid)
                .isNotNull(SpcyRsWjmb::getFcflj));

        if (CollectionUtil.isEmpty(wjmbList)) {
            wjmbList = spcyRsWjmbService.list(new LambdaQueryWrapper<SpcyRsWjmb>()
                    .eq(StringUtils.isNotBlank(fywflcode), SpcyRsWjmb::getFywflcode, fywflcode)
                    .eq(StringUtils.isNotBlank(type), SpcyRsWjmb::getFmblx1, type)
                    .like(StringUtils.isNotBlank(fmbbt), SpcyRsWjmb::getFmbbt, fmbbt)
                    .isNull(SpcyRsWjmb::getFsqsxid)
                    .isNotNull(SpcyRsWjmb::getFcflj));
        }


        Map map = new HashMap();
        map.put("wjmbList", wjmbList);

        // 是否存在已经选中的模板id
        String xzid = "";

        // 以前选择的模板id
        String oldSelectMbid = busService.getSqsxTypeFile(fsjxdid, type);
        if (StringUtils.isNotBlank(oldSelectMbid) && CollectionUtil.isNotEmpty(wjmbList)){
            for (SpcyRsWjmb wjmb : wjmbList){
                if (wjmb.getId().equals(oldSelectMbid)){
                    xzid = wjmb.getId();
                    break;
                }
            }
        }

        map.put("xzid",xzid);

        return HttpResult.ok(map);
    }

    @Log(title = "申请事项-选择模板")
    @ApiOperation(value = "申请事项-选择模板")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id",required = true),
            @ApiImplicitParam(name = "fileid",value = "文件记录id",required = true),
            @ApiImplicitParam(name = "id",value = "文件模板id",required = true),
    })
    @GetMapping(value = "selectWjmb")
    public HttpResult<?> selectWjmb(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid,
                                    @RequestParam(value = "fileid",defaultValue = "") String fileid,
                                    @RequestParam(value = "id",defaultValue = "")String wjmbid){

        Result result = busService.sqsxWjjlReloadWjmb(sqsxid, fileid, wjmbid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("操作成功");
    }

    @Log(title = "申请事项-文件在线编辑-打开文件")
    @ApiOperation(value = "申请事项-文件在线编辑-打开文件")
    @ApiImplicitParam(name = "id",value = "文件id",required = true)
    @GetMapping(value = "openWjjl")
    public void openWjjl(@RequestParam("id")String id,HttpServletResponse response){
        if (StringUtils.isBlank(id)){
            log.error("【文件在线编辑】-文件id不能为空");
            return;
        }
        SpcySqsxWjjl wjjl = spcySqsxWjjlService.getById(id);
        if (wjjl == null){
            log.error("【文件在线编辑】-文件id:{}的数据不存在",id);
            return;
        }
        if (StringUtils.isNotBlank(wjjl.getFcflj())){
            localFileUploadUtils.ejzLocalFileURl(wjjl.getFcflj(),response);
        }else {
            log.error("【文件在线编辑】- 文件存放路径不存在");
            return;
        }
    }

    @Log(title = "申请事项-在线编辑-保存文件")
    @ApiOperation(value = "申请事项-在线编辑-保存文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "docfile",value = "保存的文件",required = true),
            @ApiImplicitParam(name = "id",value = "文件id",required = true)
    })
    @PostMapping(value = "onLineSaveWjjl")
    public HttpResult<?> onLineSaveWjjl(@RequestParam("docfile") MultipartFile file,String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("文件id不能为空");
        }
        SpcySqsxWjjl wjjl = spcySqsxWjjlService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(wjjl);

        String oldCflj = wjjl.getFcflj(); // 旧文件存放路径

        String originalFilename = file.getOriginalFilename();

        if (originalFilename.lastIndexOf(".") == 0){
            return HttpResult.error("文件不存在这个格式，请联系管理员");
        }
        // 文件类型
        String wjlx = ".doc";
        // 文件名

        String sjmc = GetUuIdUtils.NotReplaceUuId();
        String wjccmc = sjmc + wjlx;
        String nowDay = DateConversionUtils.DateToMonthTwo(new Date()); // 获取当前日期 yyyy-MM-dd
        String newDir = "/"+nowDay;

        File newFile = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(newDir));
        if (!newFile.exists()){
            newFile.mkdirs();
        }

        String cflj = newDir + "/" + wjccmc;
        String orgUrl = "";

        localFileUploadUtils.uploadOnlineFile(file, sjmc,wjlx); // 上传文件

        orgUrl = localFileUploadUtils.getSqsxWjjlAbsUrl(cflj);

        String wordText = InsertAndOutWordUtils.getWordText(orgUrl, wjlx); // 获取文件内容
        if (StringUtils.isNotBlank(wordText)){
            wordText = wordText.replace(BusinessConstant.HNCA_WJZ," ");
        }

        wjjl.setFwjnr(wordText);
        wjjl.setFwjlx(wjlx);
        wjjl.setFwjccmc(wjccmc);
        wjjl.setFcflj(cflj);
        spcySqsxWjjlService.updateById(wjjl);

        String log = "文件在线编辑: ";
        log += LogUtils.BeanModifyMsg(wjjl,oldMap);
        spcyLogService.addLogs(LogConstant.LOGS_SQSX_WJJL,log,id,LogConstant.LOGS_SQSX_WJJL,SpcyConstant.LOGS_SYS_TYPE_XG);

        localFileUploadUtils.deleteLocalWjmbFileUrl(oldCflj); // 删除旧的文件

        return HttpResult.ok("保存文件成功");
    }

    @Log(title = "在线编辑-将文件保存为PDF并盖章-通过文件id")
    @ApiOperation(value = "在线编辑-将文件保存为PDF并盖章-通过文件id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id",required = true),
            @ApiImplicitParam(name = "id",value = "文件id",required = true)
    })
    @PostMapping(value = "saveFileGzToPdf")
    public void saveFileGzToPdf(HttpServletRequest request){
        String sqsxid = request.getParameter("sqsxid");
        String fileId = request.getParameter("id");
        SpcySqsxWjjl oldWjjl = spcySqsxWjjlService.getById(fileId);
        if (oldWjjl == null){
            return;
        }
        String nowNodeName = "";
        if (StringUtils.isNotBlank(sqsxid)){
            nowNodeName = busService.getNowNodeName(sqsxid);
        }

        String nowDay = DateUtil.today(); // 获取当前日期 yyyy-MM-dd
        String newDir = "/"+nowDay;

        File newFile = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(newDir));
        if (!newFile.exists()){
            newFile.mkdirs();
        }

        String fileName = GetUuIdUtils.NotReplaceUuId(); // 虚拟名称
        String fileType = ".pdf";
        fileName += fileType;
        String xnUrl = newDir + "/" +fileName;

        String uploadUrl = localFileUploadUtils.getSqsxWjjlAbsUrl(xnUrl);
        ServletInputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
        } catch (IOException e) {
            log.error("将文件保存word为PDF失败【IOException】：{}",e.getMessage());
        }
        localFileUploadUtils.uploadEjzFile(inputStream,uploadUrl); // 上传文件

        // minio
        if (localFileUploadUtils.isEnableMinioWjjlFile()){
            spcyMinioUtils.putFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(xnUrl),new File(uploadUrl),fileType);
        }

        // 修改文件地址
        if (oldWjjl != null){
            String oldWjxsmc = oldWjjl.getFwjxsmc();
            String oldCflj = oldWjjl.getFcflj();
            String oldWjccmc = oldWjjl.getFwjccmc();
            String oldWjlx = oldWjjl.getFwjlx();

            oldWjjl.setFwjlx(fileType);
            oldWjjl.setFcflj(xnUrl);
            oldWjjl.setFwjccmc(fileName);
            oldWjjl.setFjdcflj(oldCflj); // 旧的存放路径
            oldWjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YSCPDF);
            spcySqsxWjjlService.updateById(oldWjjl);


            // 方案盖骑缝章
            if (StringUtils.isNotBlank(oldWjjl.getFwjlx1()) && (oldWjjl.getFwjlx1().equals(WjmlConstant.jcfa)
                    || oldWjjl.getFwjlx1().equals(WjmlConstant.spfa))) {
                hnCaUtils.fileCaByGJZ(oldWjjl.getId()); // 文件盖章
            }else {
                hnCaUtils.fileCaByGJZ(oldWjjl.getId()); // 文件盖章
            }

            String operContent = "【文件名】为["+oldWjxsmc+"];" +
                    "【文件路径】从["+oldCflj+"]设置为["+xnUrl+"];" +
                    "【文件虚拟名】从["+oldWjccmc+"]设置为["+fileName+"];" +
                    "【文件类型】从["+oldWjlx+"]设置为["+fileType+"]";
            spcyLogService.addLogs(nowNodeName,operContent,sqsxid,LogConstant.LOGS_SQSX_WJJL,SpcyConstant.LOGS_SYS_TYPE_XG);
        }

    }

    @Log(title = "在线编辑-将文件保存为PDF并盖章-以文件形式")
    @ApiOperation(value = "在线编辑-将文件保存为PDF并盖章-以文件形式")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id",required = true),
            @ApiImplicitParam(name = "id",value = "文件id",required = true)
    })
    @PostMapping(value = "saveFileGzToPdf2")
    public void saveFileGzToPdf(@RequestParam("pdffile") MultipartFile file,
                                @RequestParam("docfile") MultipartFile file2,
                                @RequestParam("id") String fileId,
                                @RequestParam("sqsxid") String sqsxid,
                                @RequestParam(value = "filename",defaultValue = "")String filename,
                                HttpServletRequest request){
//        String sqsxid = request.getParameter("sqsxid");
//        String fileId = request.getParameter("id");
//        System.out.println(filename);
        SpcySqsxWjjl oldWjjl = spcySqsxWjjlService.getById(fileId);
        if (oldWjjl == null){
            return;
        }
        String nowNodeName = "";
        if (StringUtils.isNotBlank(sqsxid)){
            nowNodeName = busService.getNowNodeName(sqsxid);
        }

        String nowDay = DateConversionUtils.DateToMonthTwo(new Date()); // 获取当前日期 yyyy-MM-dd
        String newDir = "/"+nowDay;

        File newFile = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(newDir));
        if (!newFile.exists()){
            newFile.mkdirs();
        }

        String fileName = GetUuIdUtils.NotReplaceUuId(); // 虚拟名称
        String sjmc = fileName;
        String fileType = ".pdf";
        fileName += fileType;
        String xnUrl = newDir + "/" +fileName;

        localFileUploadUtils.uploadOnlineFile(file, sjmc,fileType); // 上传文件

        // 修改文件地址
        if (oldWjjl != null){
            String oldWjxsmc = oldWjjl.getFwjxsmc();
            String oldCflj = oldWjjl.getFcflj();
            String oldWjccmc = oldWjjl.getFwjccmc();
            String oldWjlx = oldWjjl.getFwjlx();

            oldWjjl.setFwjlx(fileType);
            oldWjjl.setFcflj(xnUrl);
            oldWjjl.setFwjccmc(fileName);
            oldWjjl.setFjdcflj(oldCflj); // 旧的存放路径
            oldWjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YSCPDF);
            spcySqsxWjjlService.updateById(oldWjjl);

            // 方案盖骑缝章
            if (StringUtils.isNotBlank(oldWjjl.getFwjlx1()) && (oldWjjl.getFwjlx1().equals(WjmlConstant.jcfa)
                    || oldWjjl.getFwjlx1().equals(WjmlConstant.spfa))) {
                hnCaUtils.fileCaByGJZ(oldWjjl.getId()); // 文件盖章
            }else {
                hnCaUtils.fileCaByGJZ(oldWjjl.getId()); // 文件盖章
            }

            String operContent = "【文件名】为["+oldWjxsmc+"];" +
                    "【文件路径】从["+oldCflj+"]设置为["+xnUrl+"];" +
                    "【文件虚拟名】从["+oldWjccmc+"]设置为["+fileName+"];" +
                    "【文件类型】从["+oldWjlx+"]设置为["+fileType+"]";
            spcyLogService.addLogs(nowNodeName,operContent,sqsxid,LogConstant.LOGS_SQSX_WJJL,SpcyConstant.LOGS_SYS_TYPE_XG);
        }

    }

    @Log(title = "在线编辑-将文件保存为PDF-通过文件id")
    @ApiOperation(value = "在线编辑-将文件保存为PDF-通过文件id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id",required = true),
            @ApiImplicitParam(name = "id",value = "文件id",required = true)
    })
    @PostMapping(value = "saveFileToPdf")
    public void saveFileToPdf(HttpServletRequest request){
        String sqsxid = request.getParameter("sqsxid");
        String fileId = request.getParameter("id");
        SpcySqsxWjjl oldWjjl = spcySqsxWjjlService.getById(fileId);
        if (oldWjjl == null){
            return;
        }
        String nowNodeName = "";
        if (StringUtils.isNotBlank(sqsxid)){
            nowNodeName = busService.getNowNodeName(sqsxid);
        }

        String nowDay = DateUtil.today(); // 获取当前日期 yyyy-MM-dd
        String newDir = "/"+nowDay;

        File newFile = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(newDir));
        if (!newFile.exists()){
            newFile.mkdirs();
        }

        String fileName = GetUuIdUtils.NotReplaceUuId(); // 虚拟名称
        String fileType = ".pdf";
        fileName += fileType;
        String xnUrl = newDir + "/" +fileName;

        String uploadUrl = localFileUploadUtils.getSqsxWjjlAbsUrl(xnUrl);
        ServletInputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
        } catch (IOException e) {
            log.error("将文件保存word为PDF失败【IOException】：{}",e.getMessage());
        }
        localFileUploadUtils.uploadEjzFile(inputStream,uploadUrl); // 上传文件

        // minio
        if (localFileUploadUtils.isEnableMinioWjjlFile()){
            spcyMinioUtils.putFile(localFileUploadUtils.getSqsxWjjlRelativeUrl(xnUrl),new File(uploadUrl),fileType);
        }

        // 修改文件地址
        if (oldWjjl != null){
            String oldWjxsmc = oldWjjl.getFwjxsmc();
            String oldCflj = oldWjjl.getFcflj();
            String oldWjccmc = oldWjjl.getFwjccmc();
            String oldWjlx = oldWjjl.getFwjlx();
            String fgzzt = oldWjjl.getFgzzt();
            // 判断盖章
            if (StringUtils.isNotBlank(fgzzt) && fgzzt.equals(SpcyConstant.WJJL_ZT_GZ)){
                oldWjjl.setFgzzt(SpcyConstant.WJJL_ZT_GZ_NO);
            }

            oldWjjl.setFwjlx(fileType);
            oldWjjl.setFcflj(xnUrl);
            oldWjjl.setFwjccmc(fileName);
            oldWjjl.setFjdcflj(oldCflj);
            oldWjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YSCPDF);
            spcySqsxWjjlService.updateById(oldWjjl);

            String operContent = "生成PDF文件:【文件名】为["+oldWjxsmc+"];" +
                    "【文件路径】从["+oldCflj+"]设置为["+xnUrl+"];" +
                    "【文件虚拟名】从["+oldWjccmc+"]设置为["+fileName+"];" +
                    "【文件类型】从["+oldWjlx+"]设置为["+fileType+"]";
            spcyLogService.addLogs(nowNodeName,operContent,sqsxid,LogConstant.LOGS_SQSX_WJJL,SpcyConstant.LOGS_SYS_TYPE_XG);
        }

    }

    @Log(title = "在线编辑-将文件保存为PDF-通过文件id")
    @ApiOperation(value = "在线编辑-将文件保存为PDF-通过文件id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id",required = true),
            @ApiImplicitParam(name = "id",value = "文件id",required = true)
    })
    @PostMapping(value = "saveFileToPdf2")
    public void saveFileToPdf(@RequestParam("pdffile") MultipartFile file,
                              @RequestParam("docfile") MultipartFile file2,
                              @RequestParam("id") String fileId,
                              @RequestParam("sqsxid") String sqsxid,
                              @RequestParam(value = "filename",defaultValue = "")String filename,
                              HttpServletRequest request){
//        String sqsxid = request.getParameter("sqsxid");
//        String fileId = request.getParameter("id");
        SpcySqsxWjjl oldWjjl = spcySqsxWjjlService.getById(fileId);
        if (oldWjjl == null){
            return;
        }
        String nowNodeName = "";
        if (StringUtils.isNotBlank(sqsxid)){
            nowNodeName = busService.getNowNodeName(sqsxid);
        }

        String nowDay = DateConversionUtils.DateToMonthTwo(new Date()); // 获取当前日期 yyyy-MM-dd
        String newDir = "/"+nowDay;

        File newFile = new File(localFileUploadUtils.getSqsxWjjlAbsUrl(newDir));
        if (!newFile.exists()){
            newFile.mkdirs();
        }

        String fileName = GetUuIdUtils.NotReplaceUuId(); // 虚拟名称
        String sjmc = fileName;
        String fileType = ".pdf";
        fileName += fileType;
        String xnUrl = newDir + "/" +fileName;

        localFileUploadUtils.uploadOnlineFile(file, sjmc,fileType); // 上传文件

        // 修改文件地址
        if (oldWjjl != null){
            String oldWjxsmc = oldWjjl.getFwjxsmc();
            String oldCflj = oldWjjl.getFcflj();
            String oldWjccmc = oldWjjl.getFwjccmc();
            String oldWjlx = oldWjjl.getFwjlx();
            String fgzzt = oldWjjl.getFgzzt();
            // 判断盖章
            if (StringUtils.isNotBlank(fgzzt) && fgzzt.equals(SpcyConstant.WJJL_ZT_GZ)){
                oldWjjl.setFgzzt(SpcyConstant.WJJL_ZT_GZ_NO);
            }

            oldWjjl.setFwjlx(fileType);
            oldWjjl.setFcflj(xnUrl);
            oldWjjl.setFwjccmc(fileName);
            oldWjjl.setFjdcflj(oldCflj);
            oldWjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YSCPDF);
            spcySqsxWjjlService.updateById(oldWjjl);

            String operContent = "生成PDF文件:【文件名】为["+oldWjxsmc+"];" +
                    "【文件路径】从["+oldCflj+"]设置为["+xnUrl+"];" +
                    "【文件虚拟名】从["+oldWjccmc+"]设置为["+fileName+"];" +
                    "【文件类型】从["+oldWjlx+"]设置为["+fileType+"]";
            spcyLogService.addLogs(nowNodeName,operContent,sqsxid,LogConstant.LOGS_SQSX_WJJL,SpcyConstant.LOGS_SYS_TYPE_XG);
        }

    }

    @Log(title = "重新编辑PDF文件")
    @ApiOperation(value = "重新编辑PDF文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "id",name = "文件id",required = true)
    })
    @GetMapping(value = "againEditPdfFile")
    public HttpResult<?> againEditPdfFile(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringUtils.isBlank(id)){
            log.error("文件参数不能为空");
            return HttpResult.error("文件参数不能为空");
        }

        SpcySqsxWjjl wjjl = spcySqsxWjjlService.getById(id);
        if (wjjl == null){
            log.error("文件记录不存在");
            return HttpResult.error("文件记录不存在");
        }

        String nowNodeName = "";
        if (StringUtils.isNotBlank(wjjl.getFpid())){
            nowNodeName = busService.getNowNodeName(wjjl.getFpid());
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(wjjl);

        String fjdcflj = wjjl.getFjdcflj(); // 旧的文件路径
        String oldWjlx = Util.getWjHz(fjdcflj); // 旧的文件类型

        if (StringUtils.isNotBlank(wjjl.getFcflj())) {
            localFileUploadUtils.deleteLocalFileUrl(wjjl.getFcflj()); // 删除文件
        }

        wjjl.setFwjlx(oldWjlx);
        wjjl.setFcflj(fjdcflj);
        wjjl.setFgzzt(SpcyConstant.WJJL_ZT_GZ_NO);
        wjjl.setFdxzt(SpcyConstant.WJJL_ZT_DX_NO);
        wjjl.setFyxzt(SpcyConstant.WJJL_ZT_YX_NO);
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);
        spcySqsxWjjlService.updateById(wjjl);

        String logNr = "重新编辑PDF文件"+wjjl.getFwjlx1()+"：文件名【"+wjjl.getFwjxsmc()+"】;";
        logNr += LogUtils.BeanModifyMsg(wjjl,oldMap);
        spcyLogService.addLogs(nowNodeName,logNr,wjjl.getFpid(),LogConstant.LOGS_SQSX_WJJL,SpcyConstant.LOGS_SYS_TYPE_XG);

        /*if (StringUtils.isNotBlank(fjdcflj)){
            localFileUploadUtils.ejzLocalFileURl(fjdcflj,response);
        }else {
            log.error("【文件在线编辑】- 文件存放路径不存在");
            return;
        }*/
        Map map = new HashMap();
        map.put("id",wjjl.getId());
        return HttpResult.ok(map);
    }

    @Log(title = "重新编辑PDF文件-并输出文件")
    @ApiOperation(value = "重新编辑PDF文件-并输出文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "id",name = "文件id",required = true)
    })
    @GetMapping(value = "againEditOutPdfFile")
    public void againEditOutPdfFile(@RequestParam("id")String id,HttpServletResponse response){
        if (StringUtils.isBlank(id)){
            log.error("文件参数不能为空");
//            return HttpResult.error("文件参数不能为空");
        }

        SpcySqsxWjjl wjjl = spcySqsxWjjlService.getById(id);
        if (wjjl == null){
            log.error("文件记录不存在");
//            return HttpResult.error("文件记录不存在");
        }

        String nowNodeName = "";
        if (StringUtils.isNotBlank(wjjl.getFpid())){
            nowNodeName = busService.getNowNodeName(wjjl.getFpid());
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(wjjl);

        if (StringUtils.isBlank(wjjl.getFjdcflj())){
            log.error("重新编辑PDF文件-并输出文件: 旧的文件路径为空");
            return;
        }
        String fjdcflj = wjjl.getFjdcflj(); // 旧的文件路径
        String oldWjlx = Util.getWjHz(fjdcflj); // 旧的文件类型

        if (StringUtils.isNotBlank(wjjl.getFcflj())) {
            localFileUploadUtils.deleteLocalFileUrl(wjjl.getFcflj()); // 删除文件
        }

        wjjl.setFwjlx(oldWjlx);
        wjjl.setFcflj(fjdcflj);
        wjjl.setFgzzt(SpcyConstant.WJJL_ZT_GZ_NO);
        wjjl.setFdxzt(SpcyConstant.WJJL_ZT_DX_NO);
        wjjl.setFyxzt(SpcyConstant.WJJL_ZT_YX_NO);
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);
        spcySqsxWjjlService.updateById(wjjl);

        String logNr = "重新编辑PDF文件"+wjjl.getFwjlx1()+"：文件名【"+wjjl.getFwjxsmc()+"】;";
        logNr += LogUtils.BeanModifyMsg(wjjl,oldMap);
        spcyLogService.addLogs(nowNodeName,logNr,wjjl.getFpid(),LogConstant.LOGS_SQSX_WJJL,SpcyConstant.LOGS_SYS_TYPE_XG);

        if (StringUtils.isNotBlank(fjdcflj)){
            localFileUploadUtils.ejzLocalFileURl(fjdcflj,response);
        }else {
            log.error("【文件在线编辑】- 文件存放路径不存在");
            return;
        }
//        Map map = new HashMap();
//        map.put("id",wjjl.getId());
//        return HttpResult.ok(map);
    }
}
